# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: RedTide <redtid3@gmail.com>

pkgbase=sfizz
pkgname=(
  sfizz
  sfizz-{lib,lv2,standalone,vst3}
  pd-sfizz
)
pkgver=1.2.1
pkgrel=2
pkgdesc="SFZ library and LV2 plugin"
arch=(loong64 x86_64)
url="https://github.com/sfztools/sfizz"
license=(BSD)
makedepends=(
  abseil-cpp
  cairo
  cmake
  cxxopts
  gcc-libs
  ghc-filesystem
  glibc
  jack
  freetype2
  libx11
  libxkbcommon
  libxkbcommon-x11
  lv2
  pango
  pugixml
  simde
  vst3sdk
  xcb-util
  xcb-util-cursor
  xcb-util-keysyms
)
checkdepends=(
  catch2
  lv2lint
)
source=(
  $url/releases/download/$pkgver/$pkgname-$pkgver.tar.gz
  https://github.com/sfztools/sfizz-ui/commit/a197e3a7.patch
  53d796ff.patch
)
sha512sums=('933f43a397f717490f9067009a5c6d43391d697d7788f07a68fbd03dd4702fb17a2ebc69fa21775c6ba927bfbed1bf1de2ff49b5816dead1a47fa90eb4372b96'
            '77853e0c0c3ea6e40ebbbefd62c85365a55f8188359b81f39c5c10b7ba9904b32f2915de771e25aa1fe821757027df957ea6ebf4ff4409c6c4807a9699bcec4b'
            '1f04ae5df7cf739801a5c796e57ca5eb0d632a61aac56f0aae0f16304a3e776b69d4926e299bbcb1f58e2f5c30602d6485a23aae41935cf7507af2d77c01fd10')
b2sums=('a8a6b512a9b001e9d9c4f39fb3576c09aaadb9132cd6852368ed137a06c95a0f3a935ef9731b291c7f0d4175311a9c10aedbe991c53d335d5b23161901d8aa51'
        'b0218d7f60275de638b268e8e8684d8bc8b9fbf360a384892319bec65fc5bb0a4ae8dd41a97dfb8a8a4efd8f127f7927e3bc0564210e2a2a3f9b5f8b129644a2'
        '8393697e7de9fd59104ec9db73214f8046aa3a3ae4f24beee9284ce19ca7c4a2cb795eccd62ecc3c07c9f671822bead4e67501db38e649c56cbeb173c66b34dd')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  # symlink tests data to top-level location so that tests can get to them (we build out of tree)
  ln -sv $pkgbase-$pkgver/tests .

  # fix build with newer vst3
  patch -d $pkgbase-$pkgver -p1 < a197e3a7.patch
  patch -d $pkgbase-$pkgver -p1 < 53d796ff.patch
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_CXX_STANDARD=17
    -D SFIZZ_PUREDATA=ON
    -D SFIZZ_TESTS=ON
    -D SFIZZ_USE_SYSTEM_ABSEIL=ON
    -D SFIZZ_USE_SYSTEM_CATCH=ON
    -D SFIZZ_USE_SYSTEM_CXXOPTS=ON
    -D SFIZZ_USE_SYSTEM_GHC_FS=ON
    -D SFIZZ_USE_SYSTEM_LV2=ON
    -D SFIZZ_USE_SYSTEM_PUGIXML=ON
    -D SFIZZ_USE_SYSTEM_SIMDE=ON
    -D SFIZZ_USE_SYSTEM_VST3SDK=ON
    -S $pkgbase-$pkgver
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

check() {
  ctest --test-dir build --output-on-failure
  lv2lint -Mpack -I build/$pkgbase.lv2 "http://sfztools.github.io/sfizz"
}

package_sfizz() {
  depends=(
    sfizz-{lib,lv2,standalone,vst3}
    pd-sfizz
  )

  DESTDIR="$pkgdir" cmake --install build

  (
    cd "$pkgdir"

    _pick $pkgbase-lib usr/include
    _pick $pkgbase-lib usr/lib/lib$pkgbase.so*
    _pick $pkgbase-lib usr/lib/pkgconfig/$pkgbase.pc

    _pick $pkgbase-lv2 usr/lib/lv2

    _pick $pkgbase-standalone usr/bin
    _pick $pkgbase-standalone usr/share/man/man1

    _pick $pkgbase-vst3 usr/lib/vst3

    _pick pd-$pkgbase usr/lib/pd
  )

}

package_pd-sfizz() {
  pkgdesc+=" - pd external"
  groups=(
    pd-externals
    pro-audio
  )
  depends=(
    abseil-cpp
    gcc-libs
    glibc
    pd
    pugixml
  )

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,CONTRIBUTING,GOVERNANCE,README}.md -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_sfizz-lib() {
  pkgdesc+=" - library"
  depends=(
    abseil-cpp
    gcc-libs
    glibc
    pugixml
  )
  provides=(libsfizz.so)

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,CONTRIBUTING,GOVERNANCE,README}.md -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_sfizz-lv2() {
  pkgdesc+=" - LV2 plugin"
  groups=(
    lv2-plugins
    pro-audio
  )
  depends=(
    abseil-cpp
    cairo
    fontconfig
    gcc-libs
    glib2
    glibc
    libxcb
    libxkbcommon
    libxkbcommon-x11
    lv2-host
    pango
    pugixml
    ttf-roboto
    xcb-util
    xcb-util-cursor
    zenity
  )

  mv -v $pkgname/* "$pkgdir"
  # devendor ttf-roboto
  ln -svf /usr/share/fonts/TTF/Roboto-Regular.ttf "$pkgdir/usr/lib/lv2/$pkgbase.lv2/Contents/Resources/Fonts/"
  install -vDm 644 $pkgbase-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,CONTRIBUTING,GOVERNANCE,README}.md -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_sfizz-standalone() {
  pkgdesc+=" - standalone"
  groups=(
    pro-audio
  )
  depends=(
    abseil-cpp
    gcc-libs
    glibc
    pugixml
  )
  optdepends=(
    'jack: for sfizz_jack'
  )

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,CONTRIBUTING,GOVERNANCE,README}.md -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_sfizz-vst3() {
  pkgdesc+=" - VST3 plugin"
  groups=(
    pro-audio
    vst3-plugins
  )
  depends=(
    abseil-cpp
    cairo
    fontconfig
    gcc-libs
    glib2
    glibc
    libxcb
    libxkbcommon
    libxkbcommon-x11
    pango
    pugixml
    ttf-roboto
    vst3-host
    xcb-util
    xcb-util-cursor
    zenity
  )

  mv -v $pkgname/* "$pkgdir"
  # devendor ttf-roboto
  ln -svf /usr/share/fonts/TTF/Roboto-Regular.ttf "$pkgdir/usr/lib/vst3/$pkgbase.vst3/Contents/Resources/Fonts/"
  install -vDm 644 $pkgbase-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,CONTRIBUTING,GOVERNANCE,README}.md -t "$pkgdir/usr/share/doc/$pkgname/"
}
